home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 1 / CU Amiga Magazine CD-ROM Special Edition (1995)(EMAP Images)(GB)[Issue 1995-11].iso / Aminet / comm / tcp / AmiTCPsdk_40.lha / AmiTCP-4.0 / src / netlib / _write.c < prev    next >
C/C++ Source or Header  |  1994-09-29  |  2KB  |  111 lines

  1. RCS_ID_C="$Id: _write.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _write.c - write() for both files and sockets (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <dos/dos.h>
  17. #include <proto/dos.h>
  18.  
  19. #include <bsdsocket.h>
  20.  
  21. extern int __io2errno(long);
  22.  
  23. int
  24. __write(int fd, const void *buffer, unsigned int length)
  25. {
  26.   struct UFB *ufb;
  27.   int         count, totcount;
  28.   char       *ptr;
  29.  
  30.   /*
  31.    * Check for the break signals
  32.    */
  33.   __chkabort();
  34.   /*
  35.    * find the ufb *
  36.    */
  37.   if ((ufb = __chkufb(fd)) == NULL) {
  38.     errno = EINVAL;
  39.     return -1;
  40.   }
  41.   /*
  42.    * Check if write is allowed
  43.    */
  44.   if (!(ufb->ufbflg & UFB_WA)) {
  45.     _OSERR = ERROR_WRITE_PROTECTED;
  46.     errno = EIO;
  47.     return -1;
  48.   }
  49.  
  50.   /*
  51.    * Seek to end of the file if necessary
  52.    */
  53.   if (ufb->ufbflg & UFB_APP)
  54.     __lseek(fd, 0, 2);
  55.  
  56.   /*
  57.    * Check if translation is not needed
  58.    */
  59.   if (!(ufb->ufbflg & UFB_XLAT) ||
  60.       (ptr = memchr(buffer, 0x0A, length)) == NULL) {
  61.     if (ufb->ufbflg & UFB_SOCK) {
  62.       if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  63.     return -1;
  64.     }
  65.     else {
  66.       if ((count = Write(ufb->ufbfh, (void *)buffer, length)) == -1)
  67.     goto osfail;
  68.     }
  69.     return count;
  70.   }
  71.  
  72.   totcount = length;
  73.  
  74.   /*
  75.    * Translate, ie., append CR before each LF
  76.    */
  77.   do {
  78.     count = ptr - (char *)buffer;
  79.     if (ufb->ufbflg & UFB_SOCK) {
  80.       if (send(fd, (char *)buffer, count, 0) < 0)
  81.     return -1;
  82.       if (send(fd, "\015"/* CR */, 1, 0) < 0)
  83.     return -1;
  84.     }
  85.     else {
  86.       if (Write(ufb->ufbfh, (void *)buffer, count) == -1)
  87.     goto osfail;
  88.       if (Write(ufb->ufbfh, "\015"/* CR */, 1) == -1)
  89.     goto osfail;
  90.     }
  91.     length -= count;
  92.     
  93.     buffer = ptr;
  94.   } while ((ptr = memchr((char *)buffer + 1, 0x0A, length)) != NULL);
  95.   
  96.   if (ufb->ufbflg & UFB_SOCK) {
  97.     if ((count = send(fd, (char *)buffer, length, 0)) < 0)
  98.       return -1;
  99.   }
  100.   else {
  101.     if (Write(ufb->ufbfh, (void *)buffer, length) == -1)
  102.       goto osfail;
  103.   }
  104.  
  105.   return totcount;
  106.  
  107. osfail:
  108.   errno = __io2errno(_OSERR = IoErr());
  109.   return -1;
  110. }
  111.